
function  game(){
    var  a = 2;
    $(".anniu").click(function(){
        if(a == 2){
            $(".rules").show();
            a=1;
            $(".anniu").attr('src','imges/Shut .png');
        }else if(a == 1){
            $(".rules").hide();
            a=2;
            $(".anniu").attr('src','imges/anniu.png');
        }
    })
}
game();
$(".share").click(function(){
    $(".share").slideUp("slow");
});
function  down(){
    $(".down").click(function(){
        $(".mask").slideUp("slow");
    });
    $(".down_2").click(function(){
        $(".mask_1").slideUp("slow");
    });
    $("#cancel").click(function(){
        $(".Conversion").slideUp("slow");
    });
    $(".Myprize").click(function () {
        $(".Conversion").show("slow");
    });
    $("#Myjiang").click(function(){
        $(".mask_1").fadeToggle("slow","linear");
    });
    $(".MYshare").click(function(){
        $(".share").toggle("slow");
    });
    $(".Gohome").click(function(){
        $(".mask_2").toggle("slow");
    });
    $(".down_1").click(function(){
        $(".mask_2").slideUp("slow");
    });
    /* $("#ensure").click(function(){
     var convert = $("#convert").val();
     if(convert == ""){
     toastShow('输入不能为空!');
     }else{
     // $.get("test.cgi", function(data){});

     alert(convert)
     }
     })*/
}
down();





$(function() {
    function getCookie(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + '=');
            if (c_start !== -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end === -1) {
                    c_end = document.cookie.length;
                }
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return '';
    };

    var userWechatId = localStorage.quanlinOpenid || 'oPHvcjvDAGO4sJECojmVqQmWphbY';
    var builderId = location.search.replace(/\?id=(\d+).*/,'$1') || '';
    if (userWechatId === '') {
        userWechatId = getCookie('openid') || '';
        if (userWechatId === '') {
            location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx240e4e72ff69ce3c&redirect_uri=http%3a%2f%2fclient.quanlin.qing-ge.cn%2fwechatNotify%2fauthorize&response_type=code&scope=snsapi_userinfo&state=event02.index#wechat_redirect';
            return;
        }
        localStorage.quanlinOpenid = userWechatId;
    }
    console.log(userWechatId);

    var HOST = 'http://e.quanlin.qing-ge.cn/quanlin_event02/';
    var chance ="";

    var drawEnable = true;
    $.ajax({
        url: (HOST+'/login/'),
        type: 'GET',
        dataType: 'json',
        data: {
            openid  : userWechatId
        },
        success:function(res){
            wx.ready(function(){
				wx.onMenuShareTimeline({
					title: 'title',
					link: 'http://e.quanlin.qing-ge.cn/quanlin_event02/index.html' + '?id=' + res.data,
					imgUrl: 'http://qing-ge.oss-cn-shenzhen.aliyuncs.com/quanlin/event01/pic.jpg',
					success: function() {

					},
					cancel: function() {}
				});
				wx.onMenuShareAppMessage({
					title: 'title',
					desc: 'desc',
					link: 'http://e.quanlin.qing-ge.cn/quanlin_event02/index.html' + '?id=' + res.data,
					imgUrl: 'http://qing-ge.oss-cn-shenzhen.aliyuncs.com/quanlin/event01/pic.jpg',
					type: 'link',
					success: function() {

					},
					cancel: function() {}
				});
			});
				
            if(res.code === 1 ){
                //頭像
               number();
                //獎品列表
                opportunity();
                //参与的总人数
                add();
                
                //参与的人数
                $.ajax({
                    url: (HOST+'/public/stats'),
                    type: 'GET',
                    dataType: 'json',
                    success:function(res){
                        //参与的人数
                        $("#participation").text(res.data.userHasDrawnCount);
                    }
                });
            }
        }
    });
    function add(){
        //最新的中奖信息
        $.ajax({
            url: (HOST+'/public/winnings/'),
            type: 'GET',
            dataType: 'json',
            success:function(res){
                //最新中奖信息
                var html ="";
                for(var i=0;i<res.data.length;i++){
                    html+= "<p>"+res.data[i]+"</p>"
                }
                $(".latest_information").html(html);
            }
        });

        //领取实物奖品
        






        $("#ensure").click(function(){
            var convert = $("#convert").val();
            if(convert == ""){
                toastShow('输入不能为空!');
            }else{
                $.get("test.cgi",convert,function(data){
                    toastShow('领取成功!');
                });
            }
        });
    }


    //我的獎品列表
    function opportunity(){
        $("#Myjiang").click(function(){
            $.ajax({
                url: (HOST+'/main/myPrizes/'),
                type: 'GET',
                dataType: 'json',
                success:function(res){
                    //console.log(res);
                    var html="";
                    for(var i=0;i<res.data.length;i++){
                        html +="<p>"+"<span>"+res.data[i].name+"</span>"+
                            "<img src='../imges/receive.png'class='receive_1'>"+
                            "</p>"
                    }
                    $(".integral_2").html(html);
                }
            });
        })
    }
   //分享
    function number(){
      $.ajax({
            url: (HOST+'/main/user'),
            type: 'GET',
            dataType: 'json',
            success:function(res){

                //用户头像
                $("#picUrl").attr('src',res.data.picUrl);
                //我的积分
                $("#integral").text(res.data.credits);
                //我的抽奖次数
                chance=res.data.chance;
                $("#occasion").text(res.data.chance);
				
				if (builderId != res.data.id && builderId != '') {
					$.ajax({
						url: (HOST+'/main/help'),
						type: 'GET',
						dataType: 'json',
						data:{
							targetId:builderId
						},
						success:function(res) {
							if(res.code == 1){
								alert("您成功帮好友增加了一次机会!")
							} else{
							   alert(res.msg)
							}
						}
					});
				}
            }
        });
        //转盘
        function zhuanpan() {
            var bRotate = false;
            var rotateFn = function(awards, angles,callback) {

                bRotate = !bRotate;

                $('#rotate').stopRotate();

                $('#rotate').rotate({

                    angle: 0,

                    animateTo: angles + 1800,

                    duration: 6000,

                    callback: function() {
                        drawEnable = true;
                         bRotate = !bRotate;
                        if(typeof callback === 'function'){
                            callback()
                        }
                    }
                })
            };

            $('.pointer').click(function() {
                /*  if (!drawEnable) {
                 // do nothing
                 }else if(chance == 0){
                 $(".share").show();
                 }else {
                 drawEnable = false;
                 $.ajax({
                 url: (HOST+'/main/draw'),
                 type: 'GET',
                 dataType: 'json',
                 success:function(res){
                 if (res.code === 1) {
                 $("#number").text(--chance);
                 var item =rnd(0, 7); //只需要改这个数,从后端获取看是哪个节点

                 /*var coupontype = res.data;

                 if(coupontype == 1){
                 item = 1;
                 }else if(coupontype == 2){
                 item = 2;
                 }else if(coupontype == 3){
                 item = 0;
                 }else if(coupontype == 4){
                 item = 4
                 }else if(coupontype == 5){
                 item = 5;
                 }*/
                $.ajax({
                    url: (HOST+'/main/draw'),
                    type: 'GET',
                    dataType: 'json',
                    success: function (res) {
                        if(chance>0){
                            $("#occasion").text(--chance);
                        }
                        else if (chance <= 0) {
                            $("#occasion").text.val=0
                        }
                        var item =res.data.slot;
                       if (bRotate) return;
                        //                            setTimeout(preferential(),2000)
                        preferential();

                        function preferential(){
                            switch (item) {
                                case 0:
                                    //var angle = [26, 88, 137, 185, 235, 287, 337];
                                    rotateFn(0,51.42,function(){
                                       // alert("免費");
                                        $(".mask").show();
                                        $(".iphone7").attr('src',"imges/free.png");
                                    });

                                    break;
                                case 1:
                                    //var angle = [88, 137, 185, 235, 287];
                                    rotateFn(1,102.85,function(){
                                        //alert("5積分");
                                        $(".mask").show();
                                        $(".iphone7").attr('src',"imges/points_5.png");
                                    });
                                    break;
                                case 2:
                                    //var angle = [137, 185, 235, 287];
                                    rotateFn(2, 154.28,function(){
                                       // alert("蘋果");
                                        $(".mask").show();
                                        $(".iphone7").attr('src',"imges/iphone7.png");
                                    });
                                    break;
                                case 3:
                                    //var angle = [137, 185, 235, 287];
                                    rotateFn(3, 205.71,function(){
                                        //alert("一積分");
                                        $(".mask").show();
                                        $(".iphone7").attr('src',"imges/points_1.png");
                                    });
                                    break;
                                case 4:
                                    //var angle = [185, 235, 287];
                                    rotateFn(4, 257.14,function(){
                                        //alert("50積分");
                                        $(".mask").show();
                                        $(".iphone7").attr('src',"imges/points_50.png");
                                    });
                                    break;
                                case 5:
                                    //var angle = [185, 235, 287];

                                    rotateFn(5,308.57,function(){
                                        //alert("小家电");
                                        $(".mask").show();
                                        $(".iphone7").attr('src',"imges/physical.png");
                                    });
                                    break;
                                case 6:
                                    //var angle = [185, 235, 287];

                                    rotateFn(6,355,function(){
                                       // alert("十幾分");
                                        $(".mask").show();
                                        $(".iphone7").attr('src',"imges/points_10.png");
                                    });
                                    break;
                            }
                        }
                    }
                });
            });
        }
        zhuanpan();

		

        $.ajax({
            url: (HOST+'/wx/config'),
            type: 'GET',
            dataType: 'json',
            data: {
                url : location.href
            },
            success: function(res){
                if(res.code === 1){
                    res.data.jsApiList = ['checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'closeWindow'];
                    wx.config(res.data);
                }
            }
        });
    }
    //轉盤
   /* $(function() {
        var bRotate = false;
        var rotateFn = function(awards, angles,callback) {

            bRotate = !bRotate;

            $('#rotate').stopRotate();

            $('#rotate').rotate({

                angle: 0,

                animateTo: angles + 1800,

                duration: 6000,

                callback: function() {
                    drawEnable = true;
                    bRotate = !bRotate;
                    if(typeof callback === 'function'){
                        callback()
                    }
                }
            })
        };

        $('.pointer').click(function() {
            if (!drawEnable) {
                // do nothing
            }else if(chance == 0){
                $(".share").show();
            }else {
                drawEnable = false;
                $.ajax({
                    url: (HOST+'(/main/draw'),
                    type: 'GET',
                    dataType: 'json',
                    success:function(res){
                        if (res.code === 1) {
                           // $("#occasion").text(res.data.chance);
                            var item =res.data.slot; //只需要改这个数,从后端获取看是哪个节点

                            /*var coupontype = res.data;

                             if(coupontype == 1){
                             item = 1;
                             }else if(coupontype == 2){
                             item = 2;
                             }else if(coupontype == 3){
                             item = 0;
                             }else if(coupontype == 4){
                             item = 4
                             }else if(coupontype == 5){
                             item = 5;
                             }
                            if (bRotate) return;
                            //                            setTimeout(preferential(),2000)
                            preferential();

                            function preferential(){
                                switch (item) {
                                    case 0:
                                        //var angle = [26, 88, 137, 185, 235, 287, 337];
                                        rotateFn(0,51.42,function(){
                                            // alert("免費");
                                            $(".mask").show();
                                            $(".iphone7").attr('src',res.data.picUrl);
                                        });

                                        break;
                                    case 1:
                                        //var angle = [88, 137, 185, 235, 287];
                                        rotateFn(1,102.85,function(){
                                            // alert("5積分");
                                            $(".mask").show();
                                            $(".iphone7").attr('src',res.data.picUrl);
                                        });
                                        break;
                                    case 2:
                                        //var angle = [137, 185, 235, 287];
                                        rotateFn(2, 154.28,function(){
                                            // alert("蘋果");
                                            $(".mask").show();
                                            $(".iphone7").attr('src',res.data.picUrl);
                                        });
                                        break;
                                    case 3:
                                        //var angle = [137, 185, 235, 287];
                                        rotateFn(3, 205.71,function(){
                                            // alert("一積分");
                                            $(".mask").show();
                                            $(".iphone7").attr('src',res.data.picUrl);
                                        });
                                        break;
                                    case 4:
                                        //var angle = [185, 235, 287];
                                        rotateFn(4, 257.14,function(){
                                            // alert("50積分");
                                            $(".mask").show();
                                            $(".iphone7").attr('src',res.data.picUrl);
                                        });
                                        break;
                                    case 5:
                                        //var angle = [185, 235, 287];

                                        rotateFn(5,308.57,function(){
                                            // alert("實物禮");
                                            $(".mask").show();
                                            $(".iphone7").attr('src',res.data.picUrl);
                                        });
                                        break;
                                    case 6:
                                        //var angle = [185, 235, 287];

                                        rotateFn(6,355,function(){
                                            // alert("十幾分");
                                            $(".mask").show();
                                            $(".iphone7").attr('src',res.data.picUrl);
                                        });
                                        break;
                                }
                            }
                        }
                    }
                });
            }
        });
    });*/
});
